package cn.yitun.chat.yitunserverimapicore.business.group.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupCategoryDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupCategoryInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupCategoryVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupCategoryDefaultEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupCategoryService;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupCategoryDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupRelationDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupCategoryManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupCategoryPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-20 21:05:24<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Service
@Transactional
public class GroupCategoryServiceImpl
		extends CrudServiceImpl<GroupCategoryDao ,GroupCategoryDto, GroupCategoryInfo, GroupCategoryVo>
		implements GroupCategoryService {

	@Resource
	private GroupRelationDao groupRelationDAO;
	@Resource
	private GroupCategoryManager groupCategoryManager;
	@Resource
	private GroupCategoryPush groupCategoryPush;

	/**
	 * Date 2019-01-20 14:09:57<br>
	 * Description 获取所有分组
	 * @author yitun
	 * @param userId
	 * @return
	 * @since 1.0.0
	 */
	public List<GroupCategoryVo> getListByUserId(Long userId) {
		List<GroupCategoryVo> list = dao.listVO(R.param("userId", userId));
		return list;
	}

	/**
	 * 获取群分类
	 * @param userId
	 * @param id
	 * @return
	 */
	public GroupCategoryVo getVOById(Long userId, Long id) {
		return dao.getVO(id);
	}

	/**
	 * Date 2019-01-20 14:04:32<br>
	 * Description 新增分组
	 * @author yitun
	 * @param groupCategory
	 * @return
	 * @since 1.0.0
	 */
	public ResultMessage add(Long key, GroupCategoryDto groupCategory) {
		ResultMessage message = new ResultMessage();

		if (null != groupCategory) {
			if (dao.count(YitunMapUtil.java2Map(groupCategory, false)) > 0) {
				message.addError(ExceptionEnum.NAME_IS_REPETITION);
				return message;
			}

			try {

				int maxCount = 30;
				Long userId = groupCategory.getUserId();
				int count = dao.count(
						R.param("userId", userId));
				if (count >= maxCount) {
					message.addWarning(ExceptionEnum.SOCK_GROUP_CATEGORY_OVER_LIMIT);
				} else {
					int rank = count + 1;
					groupCategory.setRank(rank);
					groupCategory.setSort(GroupCategoryDefaultEnums.SORT_CUSTOM.getType());
					groupCategory.setCreateBy(userId);
					groupCategory.setUpdateBy(userId);
					dao.save(groupCategory);
					message.bodyPut("category", groupCategory);

					groupCategoryPush.pushAdd(key, groupCategory.getId(), userId);
				}
			} catch (Exception e) {
				e.printStackTrace();
				message.addWarning(ExceptionEnum.BUSY_SERVER);
			}
		} else {
			message.addWarning(ExceptionEnum.BUSY_SERVER);
		}
		return message;
	}

	/**
	 * Date 2019-01-20 14:06:12<br>
	 * Description 修改分组名称
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	public ResultMessage updateName(Long key, GroupCategoryDto query) {
		dao.update(query);
		groupCategoryPush.pushUpdateName(IdGen.getNumber(), query.getId(), query.getName(), query.getUserId());
		return new ResultMessage(R.param("category", dao.getVO(query.getId())));
	}

	/**
	 * Date 2019-01-20 17:26:21<br>
	 * Description 修改排序
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	public ResultMessage updateRank(Long key, GroupCategoryDto query) {
		ResultMessage message = new ResultMessage();

		Long userId = query.getUserId();
		Long id = query.getId();
		Integer rank = query.getRank();

		List<GroupCategoryVo> list = dao.listVO(
				R.param("userId", userId));
		int size = list.size();

		if (rank < size) {

			int temp = 0;

			for (int i = 0; i < size; i++) {
				GroupCategoryVo m = list.get(i);
				if (!m.getId().equals(id)) {
					if (temp == rank) {
						temp++;
					}
					dao.updateRank(m.getId(), temp);
					temp++;
				}
			}
			dao.updateRank(id, rank);
		}
		groupCategoryPush.pushUpdateRank(IdGen.getNumber(), userId);
		return message;
	}

	/**
	 * Date 2019-01-20 16:56:33<br>
	 * Description
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	public ResultMessage delete(Long key, GroupCategoryDto query) {
		ResultMessage message = new ResultMessage();
		Long id = query.getId();
		Long userId = query.getUserId();
		GroupCategoryVo category = dao.getVO(id);
		if (null != category) {
			if (GroupCategoryDefaultEnums.SORT_DEFAULT.getType() == category.getSort()) {
				message.addWarning(ExceptionEnum.SOCK_GROUP_CATEGORY_DEFAULT_CANT_DELETE);
			} else {
				dao.remove(id);
				Long newCategoryId = groupCategoryManager.getOrCreateDefaultCategoryId(userId);
				groupRelationDAO.updateChangeCategoryId(userId, id, newCategoryId);
				groupCategoryPush.pushDelete(IdGen.getNumber(), id, userId);
			}
		}
		return message;
	}
}
